package control;

import AddressGoods.AddressGoodsService;
import AddressGoods.IAddressGoodsService;
import control.Goods;
import control.GoodsFunction;
import control.GoodsService;
import utils.DBUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class GoodsController {
    Scanner scanner;
    //Alt+insert (Alt+fn+insert)
    GoodsService goodsService;
    IAddressGoodsService addressGoodsService;
    public GoodsController() {
        scanner = new Scanner(System.in);
        goodsService = new GoodsService();
        addressGoodsService=new AddressGoodsService();
    }
    public void startSystem() {
        try {
            Goods goods;
            String goodsId = "";//运行时输入
            do {
                GoodsMenu.runGoodsMenu();
                String userSelect = scanner.next();
                switch (userSelect) {
                    case "7":
                        String sql="insert into tb_goods values(null,?,?,?)";
                        List<Object> list=new ArrayList();
                        goods = new Goods();
                        System.out.print("请输入商品 ID:");
                        String id = scanner.next();
                        System.out.print("请输入商品名称:");
                        String goodsName = scanner.next();
                        System.out.print("请输入商品类型:");
                        String goodsType = scanner.next();
                        System.out.print("请输入商品库存:");
                        int inventory = scanner.nextInt();
                        System.out.print("请输入商品单价:");
                        double price = scanner.nextDouble();
                        goods.setGoodsId(id);
                        goods.setGoodsName(goodsName);
                        goods.setGoodsType(goodsType);
                        goods.setInventory(inventory);
                        goods.setPrice(price);
                        goodsService.addGoods(goods);
                        insert(goods);
                        break;
                    case "8":
                        System.out.print("请输入要删除的商品 ID:");
                        goodsId = scanner.next();
                        deletegood(goodsId);
                        break;
                    case "9":
                        goods = new Goods();
                        //先查再改，根据****修改
                        System.out.println("正在执行修改商品功能...");
                        System.out.println("----------------------");
                        System.out.println("请输入商品名称");
                        String name=scanner.next();
                        int i=findgood(name);
                        if(i==1){
                            System.out.println("请输入商品类型");
                            String type=scanner.next();
                            System.out.println("请输入商品id：");
                            String ID=scanner.next();
                            System.out.println("请输入商品价钱：");
                            double Price=scanner.nextDouble();
                            goods.setGoodsName(name);
                            goods.setGoodsId(ID);
                            goods.setPrice(Price);
                            goods.setGoodsType(type);
                            updategood(goods);
                        }

                        //给 goods 赋值

                        break;
                    case "10":
                        System.out.print("请输入要查询的商品 ID:");
                        goodsId = scanner.next();
                        findgood(goodsId);
                        break;
                    case "11":
                        exit();
                        break;
                }
                System.out.println("按任意键继续..."); System.in.read();
            } while (true);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("系统出错了，请重试");
        }//Ctrl+Alt+L 格式化代码
    }



    public void exit() {
        System.out.print("是否真的要退出系统？（Y|N）:");
        String userInput = scanner.next();
        if (userInput.equalsIgnoreCase("y")) {
            System.exit(0);
        }}
    public void insert(Goods goods){
        int result=addressGoodsService.insert(goods);
        if(result==1){
            System.out.println("商品添加成功！");
        }else{
            System.out.println("商品添加失败！");
        }
        /*String sql="insert into tb_user values(?,?,?,?,?)";
        List<Object> list=new ArrayList();
        list.add(goods.getGoodsId());
        list.add(goods.getGoodsType());
        list.add(goods.getGoodsName());
        list.add(goods.getPrice());
        list.add(goods.getInventory());
        DBUtils.executeUpdate(sql,list);
        int i=DBUtils.executeUpdate(sql,list);
        return i;*/
    }
    public void deletegood(String name){
        int i= addressGoodsService.deletegood(name);
        if (i>0){
            System.out.println("商品删除成功");
        }else{
            System.out.println("查无此物！");
        }
    }
    public int findgood(String name){
        Goods goods=addressGoodsService.findgood(name);
        if (goods!=null){
            System.out.println(goods);
            return 1;
        }else{
            System.out.println("查无此物！");
            return 0;
        }
    }
    public void updategood(Goods goods){
        int i=addressGoodsService.updategood(goods);
        if (i>=1){
            System.out.println("商品修改成功！");
        }else{
            System.out.println("商品修改失败！");
        }
    }

}

